package com.fary.controller;

import com.fary.po.Addr;
import com.fary.po.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Fary
 * @version 1.0
 * @description: TODO
 * @date 2022/3/13 11:49
 */
@Controller
@RequestMapping("/test")
public class InitBinderController {
    // 绑定变量名字和属性，参数封装进类
    @InitBinder("user")
    public void initBinderUser(WebDataBinder binder) {
        binder.setFieldDefaultPrefix("user.");
    }
    // 绑定变量名字和属性，参数封装进类
    @InitBinder("addr")
    public void initBinderAddr(WebDataBinder binder) {
        binder.setFieldDefaultPrefix("addr.");
    }

    @RequestMapping("/test")
    @ResponseBody
    public Map<String,Object> test(HttpServletRequest request, @ModelAttribute("user") User user,
                                   @ModelAttribute("addr") Addr addr){
        Map<String,Object> map=new HashMap<String,Object>();
        map.put("user", user);
        map.put("addr", addr);
        return map;
    }
}
